<template>
  <div id="app">
    <el-container>
      <el-header>
        <nav class="nav-menu">
          <div class="logo">
            <img src="/images/logo.png" alt="Logo">
            <span>海洋生态保护</span>
          </div>
          <el-menu mode="horizontal" router :default-active="$route.path">
            <el-menu-item index="/">首页</el-menu-item>
            <el-menu-item index="/announcements">通知公告</el-menu-item>
            <el-menu-item index="/news">新闻动态</el-menu-item>
            <el-menu-item index="/activities">志愿活动</el-menu-item>
            <el-menu-item index="/teams">志愿队伍</el-menu-item>
            <el-menu-item index="/education">环保教育</el-menu-item>
            <el-menu-item index="/donation">众筹捐助</el-menu-item>
            <el-menu-item index="/game">互动游戏</el-menu-item>
          </el-menu>
          
          <div class="user-menu">
            <template v-if="$store.state.user">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  {{ $store.state.user.name }}
                  <el-icon class="el-icon--right"><arrow-down /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                    <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
            <template v-else>
              <el-button type="primary" @click="$router.push('/login')">
                登录
              </el-button>
            </template>
          </div>
        </nav>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const router = useRouter()
const store = useStore()

const handleCommand = (command) => {
  console.log('用户信息：',store.state.user)
  const role = store.state.user.role
  switch(command) {
    case 'profile':
      switch(role) {
        case 1:
          router.push('/admin/dashboard')
          break
        case 2:
          router.push('/volunteer/home')
          break
        case 3:
          router.push('/organization/home')
          break
        default:
          router.push('/')
      }
      break
    case 'logout':
      store.dispatch('logout')
      router.push('/')
      ElMessage.success('已退出登录')
      break
  }
}
</script>

<style>
#app {
  font-family: Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
}

.el-header {
  padding: 0;
  background: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-menu {
  max-width: 1200px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  margin-right: 40px;
}

.logo img {
  height: 40px;
  margin-right: 10px;
  border-radius: 8px;
}

.logo span {
  font-size: 1.2em;
  font-weight: bold;
  color: #006994;
}

.el-menu {
  flex: 1;
  border: none;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}

.user-menu {
  margin-left: 20px;
}

.el-dropdown-link {
  cursor: pointer;
  color:  #006994;
  display: flex;
  align-items: center;
}

@media (max-width: 768px) {
  .nav-menu {
    padding: 0 10px;
  }
  
  .logo span {
    display: none;
  }
  
  .el-menu-item {
    padding: 0 10px;
  }
}
</style>
